<template>
    <div class="flex" :style="{ flexDirection: props.isColumn ? 'column' : '' }">
        <div
            :style="{
                width: isNeedLegend ? '45%' : '100%'
            }"
            :class="['chart', props.isNeedBack ? 'chart-back' : '']"
        >
            <V3Echarts :height="props.height * 0.95" :options="option" container="container3" isFirst />
        </div>
        <slot>
            <div class="legend" v-if="isNeedLegend">
                <div v-for="(item, index) in props.chartData" :key="item.name">
                    <div class="item" v-if="item.name">
                        <div class="item-left">
                            <div class="circle" :style="'background:' + props.color?.[index]"></div>
                            <div class="label">{{ item.name }}</div>
                        </div>
                        <div class="count">
                            {{ item.value }}
                            <span class="unit">{{ props.unit }}</span>
                        </div>
                        <div class="count" v-if="props.isShowPercent">
                            {{ ((item.value / sum) * 100).toFixed(0) }}
                            <span class="unit">%</span>
                        </div>
                    </div>
                </div>
            </div>
        </slot>
    </div>
</template>

<script lang="ts" setup>
import { ref, watch } from 'vue'
import * as echarts from 'echarts'
import { FontSize } from '@/stories/components/charts/chartConfig'
import V3Echarts from '@/stories/components/V3Echarts/index.vue'

interface chartDataType {
    name: string
    value: number
}

const props = defineProps({
    chartData: {
        type: Array<chartDataType>,
        default: () => {
            return [
                {
                    name: '湛江',
                    value: 60
                },
                {
                    name: '地市',
                    value: 40
                }
            ]
        }
    },
    isColumn: { type: Boolean, default: false },
    // 是否echarts显示名称
    isShowName: { type: Boolean, default: false },
    // 是否echarts显示百分比
    percent: { type: String, default: '%' },
    // 是否显示侧边
    isNeedLegend: { type: Boolean, default: true },
    // 侧边是否显示百分比
    isShowPercent: { type: Boolean, default: false },
    // 是否需要背景图
    isNeedBack: {
        type: Boolean,
        default: false
    },
    color: {
        type: Array,
        default: () => {
            return ['#a183e2', '#fe8151', '#00fffc', '#64baff', '#b4b3ca', '#f14313', '#d6a33d']
        }
    },
    // 右侧单位
    unit: {
        type: String,
        default: ''
    },
    height: {
        type: Number,
        default: 350
    }
})
let option = ref({})
let sum = 0

const initChart = () => {
    const colorCopy = JSON.parse(JSON.stringify(props.color))

    const chartDataCopy = JSON.parse(JSON.stringify(props.chartData))
    sum = chartDataCopy.reduce((prev: any, cur: any) => {
        return prev + cur.value
    }, 0)
    chartDataCopy.forEach((item: any, index: number) => {
        item.itemStyle = {
            //颜色渐变
            color: new echarts.graphic.LinearGradient(0, 0, 1, 1, [
                { offset: 0, color: colorCopy[index] },
                { offset: 1, color: 'transparent' }
            ])
        }
    })

    option.value = {
        color: colorCopy,
        series: [
            {
                type: 'pie',
                radius: ['55%', '80%'],

                label: {
                    show: false,
                    position: 'center'
                },
                labelLine: {
                    show: false
                },
                emphasis: {
                    scale: {
                        hoverAnimation: false
                    },
                    label: {
                        show: true,
                        color: '#fff',
                        fontSize: 10,
                        fontWeight: 'bold',
                        formatter: function (data: any) {
                            if (props.percent == '%') {
                                if (props.isShowName) {
                                    return '{value|' + ((data.value / sum) * 100).toFixed(2) + '}' + props.percent + '\n' + data.name
                                } else {
                                    return '{value|' + ((data.value / sum) * 100).toFixed(2) + '}' + props.percent
                                }
                            } else {
                                if (props.isShowName) {
                                    return '{value|' + data.value + '}' + props.percent + '\n' + data.name
                                } else {
                                    return '{value|' + data.value + '}' + props.percent
                                }
                            }
                        },
                        rich: {
                            value: {
                                fontFamily: 'SJyunhei',
                                fontSize: 14,
                                fontWeight: '400'
                            }
                        }
                    }
                },
                data: chartDataCopy
            }
        ]
    }
}

watch(
    () => props,
    newValue => {
        initChart()
    },
    { immediate: true, deep: true }
)
</script>

<style lang="scss" scoped>
.flex {
    width: 30%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: v-bind('props.height');
    color: #ffffff;
    .chart {
        position: relative;
        margin-right: 15px;
    }

    .chart-back {
        background: url('@/assets/images/基础/DataInfo_bg.png') no-repeat;
        background-size: 100% 100%;
    }
    .legend {
        width: 60%;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        padding-top: 10px;
        padding-bottom: 10px;
        .item {
            padding: 5px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            background: url('../img/RingChart_dataBG.png') no-repeat;
            background-size: 100% 100%;
            margin-bottom: 10px;
            .item-left {
                display: flex;
                align-items: center;
            }
            .circle {
                width: 5px;
                height: 5px;
                margin-right: 10px;
                margin-left: 10px;
            }
            .label {
                font-size: 18px;
                color: #ddd;
                margin-right: 10px;
                font-family: OPPOSans;
            }
            .count {
                font-size: 24px;
                font-family: SJyunhei;
                text-align: right;
                .unit {
                    font-size: 14px;
                    font-family: OPPOSans;
                    opacity: 0.5;
                }
            }
        }
    }
}
</style>
